/*---------------< VIET VO >---------------------*/
(function($){
  $.fn.langSelector=function(options){
	  options=$.extend({
		  langs:[],
		  def:{text:'Tiếng Việt',value:'vi', img: false},
		  callback:function(){}
	  },options);
	  $(this).each(function(){
		 
		  /*$(this).click(function(){
			  $('.lang-selector').toggle('fast');
			  return false;
		  });*/
		  var boxTpl=$('<div class="lang-inner"></div>');
		  var selector=$('<div class="selector-dropdown">'+options.def.text+'</div>');
		  var tmp = $('<div class="lang-selector"></div>');
		  
		  for(var i=0; i<options.langs.length;i++){
			  var el = null;
			  if(options.def.img){
				  el = $('<li class="lang-element'+(options.langs[i].value==options.def.value?' lang-element-active':'')+'" data-value="'+options.langs[i].value+'" ><img src="'+options.langs[i].img_path+'"/>&nbsp;&nbsp;'+options.langs[i].text+'</li>');
				  if(options.langs[i].value==options.def.value){
					  selector.html('<img src="'+options.langs[i].img_path+'"/>&nbsp;&nbsp;'+ options.langs[i].text);
				  }
			  }else{
				  el = $('<li class="lang-element'+(options.langs[i].value==options.def.value?' lang-element-active':'')+'" data-value="'+options.langs[i].value+'" >'+options.langs[i].text+'</li>');
				  if(options.langs[i].value==options.def.value){
					  selector.html(options.langs[i].text);
				  }
			  }
			 
			  el.click(function(e){
				  if(options.callback){
					  selector.html($(this).html());
					  $('.lang-element-active',boxTpl).removeClass('lang-element-active');
					  $(this).addClass('lang-element-active');
					  boxTpl.hide('fast');
					  if(selector.hasClass('selector-dropdown-active')){
						  selector.removeClass('selector-dropdown-active');
					  }else{
						  selector.addClass('selector-dropdown-active');
					  }
					  options.callback($(this).attr('data-value')).call();
					  return false;
				  };
			  });
			  boxTpl.append(el);
		  }
		  
		  selector.click(function(){
			  boxTpl.slideToggle('fast');
			  if(selector.hasClass('selector-dropdown-active')){
				  selector.removeClass('selector-dropdown-active');
			  }else{
				  selector.addClass('selector-dropdown-active');
			  }
		  });
		  tmp.append(selector);
		  tmp.append(boxTpl);
		  $(this).append(tmp);
		  //tmp.hide();
		  boxTpl.hide();
	  });
	
  };
})(jQuery);